package Service.StatisticalReport.StatisticalReportImpl;

import Dao.StatisticalReportDao.StatisticsDao;
import Model.StatisticalReport.ReportStatisticsQuery;
import Service.StatisticalReport.ReportStatisticsService;
import org.springframework.stereotype.Service;
import utils.pages.PageBean;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class ReportStatisticsServiceImpl implements ReportStatisticsService {

    @Resource
    StatisticsDao dao;

    /**
     * 格式化查询条件
     * @param query
     */
    private void formatQuery(ReportStatisticsQuery query){
        String name = query.getYYNAME();
        if(name != null && name.length() == 0){query.setYYNAME(null);}
    }

    @Override
    public PageBean<Map<String, Object>> selectNum(ReportStatisticsQuery query) {
        formatQuery(query);

        int count = query.getCount();
        List<Map<String, Object>> dataset = null;
        switch (query.getReport()){
            case DEPARTMENT:
                count =  count > 0? count: dao.countDepartmentSummary(query);
                if(count > 0){
                    dataset = dao.selectDepartmentNum(query);
                }else{
                    return null;
                }
                break;
            case HOME:
                count =  count > 0? count: dao.countHomeSummary(query);
                if(count > 0){
                    dataset = dao.selectHomeNum(query);
                }else{
                    return null;
                }
                break;
            case EVALUATE:
                count =  count > 0? count: dao.countEvaluateSummary(query);
                if(count > 0){
                    dataset = dao.selectEvaluateNum(query);
                }else{
                    return null;
                }
                break;
        }
        PageBean<Map<String, Object>> pageBean = new PageBean<>();
        pageBean.setData(dataset);
        pageBean.setCount(count);
        return pageBean;
    }

    @Override
    public PageBean<Map<String, Object>> selectTimes(ReportStatisticsQuery query) {
        formatQuery(query);

        int count = query.getCount();
        List<Map<String, Object>> dataset = null;
        switch (query.getReport()){
            case DEPARTMENT:
                count =  count > 0? count: dao.countDepartmentSummary(query);
                if(count > 0){
                    dataset = dao.selectDepartmentTimes(query);
                }else{
                    return null;
                }
                break;
            case HOME:
                count =  count > 0? count: dao.countHomeSummary(query);
                if(count > 0){
                    dataset = dao.selectHomeTimes(query);
                }else{
                    return null;
                }
                break;
            case EVALUATE:
                count =  count > 0? count: dao.countEvaluateSummary(query);
                if(count > 0){
                    dataset = dao.selectEvaluateTimes(query);
                }else{
                    return null;
                }
                break;
        }
        PageBean<Map<String, Object>> pageBean = new PageBean<>();
        pageBean.setData(dataset);
        pageBean.setCount(count);
        return pageBean;
    }

    @Override
    public PageBean<Map<String, Object>> selectExpense(ReportStatisticsQuery query) {
        formatQuery(query);

        int count = query.getCount();
        List<Map<String, Object>> dataset = null;
        switch (query.getReport()){
            case DEPARTMENT:
                count =  count > 0? count: dao.countDepartmentSummary(query);
                if(count > 0){
                    dataset = dao.selectDepartmentExpense(query);
                }else{
                    return null;
                }
                break;
            case HOME:
                count =  count > 0? count: dao.countHomeSummary(query);
                if(count > 0){
                    dataset = dao.selectHomeExpense(query);
                }else{
                    return null;
                }
                break;
            case EVALUATE:
                count =  count > 0? count: dao.countEvaluateSummary(query);
                if(count > 0){
                    dataset = dao.selectEvaluateExpense(query);
                }else{
                    return null;
                }
                break;
        }
        PageBean<Map<String, Object>> pageBean = new PageBean<>();
        pageBean.setData(dataset);
        pageBean.setCount(count);
        return pageBean;
    }

    @Override
    public PageBean<Map<String, Object>> selectPayment(ReportStatisticsQuery query) {
        formatQuery(query);

        int count = query.getCount();
        List<Map<String, Object>> dataset = null;
        switch (query.getReport()){
            case DEPARTMENT:
                count =  count > 0? count: dao.countDepartmentSummary(query);
                if(count > 0){
                    dataset = dao.selectDepartmentPayment(query);
                }else{
                    return null;
                }
                break;
            case HOME:
                count =  count > 0? count: dao.countHomeSummary(query);
                if(count > 0){
                    dataset = dao.selectHomePayment(query);
                }else{
                    return null;
                }
                break;
            case EVALUATE:
                count =  count > 0? count: dao.countEvaluateSummary(query);
                if(count > 0){
                    dataset = dao.selectEvaluatePayment(query);
                }else{
                    return null;
                }
                break;
        }
        PageBean<Map<String, Object>> pageBean = new PageBean<>();
        pageBean.setData(dataset);
        pageBean.setCount(count);
        return pageBean;
    }


    @Override
    public PageBean<Map<String, Object>> selectSettlement(ReportStatisticsQuery query) {
        if (query.getYearMonth() == null || query.getType() == null) {
            return null;
        }
        String name = query.getYYNAME();
        if(name != null && name.length() == 0){query.setYYNAME(null);}
        int count = query.getCount() > 0? query.getCount(): dao.countSettlement(query);
        if(count > 0){
            PageBean<Map<String, Object>> pageBean = new PageBean<>();
            pageBean.setData(dao.selectSettlement(query));
            pageBean.setCount(count);
            return pageBean;
        }
        return null;
    }


    @Override
    public List<Map<String, Object>> selectTotalExpense(ReportStatisticsQuery query) {
        final int MONTH = 12;
        List<Map<String, Object>> rows = dao.selectTotalExpense(query);
        return rows;
    }

    @Override
    public List<Map<String, Object>> selectMonthly(ReportStatisticsQuery query) {
        if(query.getYear() <= 0){
            return null;
        }
        return dao.selectMonthly(query);
    }
}
